package com.taokatao.cardmee.util;


import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;


public class ScrollHelper {

    public interface OnScrollStateChangedListener {
        void onScrollToBottom();
    }

    public static void init(RecyclerView recyclerView, final LinearLayoutManager layoutManager, final OnScrollStateChangedListener callback) {
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            private boolean scrollDown = false;

            @Override
            public void onScrolled(RecyclerView vRecycler, int dx, int dy) {
                super.onScrolled(vRecycler, dx, dy);
                scrollDown = dy > 0;
            }

            @Override
            public void onScrollStateChanged(RecyclerView vRecycler, int newState) {
                super.onScrollStateChanged(vRecycler, newState);
                switch (newState) {
                    case RecyclerView.SCROLL_STATE_IDLE:
                        int lastVisibleItem = layoutManager.findLastCompletelyVisibleItemPosition();
                        int totalItemCount = layoutManager.getItemCount();
                        if (lastVisibleItem >= (totalItemCount - 1) && scrollDown) {
                            if (callback != null) callback.onScrollToBottom();
                        }
                        break;
                    case RecyclerView.SCROLL_STATE_DRAGGING:
                        break;
                }
            }
        });
    }
}
